/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.core.ui;

import com.kurvykurvy.core.KurvyDataManager;
import com.kurvykurvy.page.Book;
import com.kurvykurvy.page.PageSpread;
import com.kurvykurvy.page.PageSpreadSlide;
import com.kurvykurvy.ui.layout.VerticalLayout;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.logging.Logger;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;

/**
 * Top component which displays something.
 */
@ConvertAsProperties( dtd = "-//com.kurvykurvy.core.ui//BookLayout//EN",
autostore = false )
public final class BookLayoutTopComponent 
    extends TopComponent
{

    private static BookLayoutTopComponent instance;
    
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "BookLayoutTopComponent";

    Book currentBook;

    public BookLayoutTopComponent()
    {
        initComponents();
        setName( NbBundle.getMessage( BookLayoutTopComponent.class, "CTL_BookLayoutTopComponent" ) );
        setToolTipText( NbBundle.getMessage( BookLayoutTopComponent.class, "HINT_BookLayoutTopComponent" ) );
//        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
        putClientProperty( TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE );
        putClientProperty( TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE );
        putClientProperty( TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bookLayoutStripView = new javax.swing.JScrollPane();
        bookLayoutStrip = new javax.swing.JPanel();

        bookLayoutStrip.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout bookLayoutStripLayout = new javax.swing.GroupLayout(bookLayoutStrip);
        bookLayoutStrip.setLayout(bookLayoutStripLayout);
        bookLayoutStripLayout.setHorizontalGroup(
            bookLayoutStripLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 210, Short.MAX_VALUE)
        );
        bookLayoutStripLayout.setVerticalGroup(
            bookLayoutStripLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 863, Short.MAX_VALUE)
        );

        bookLayoutStripView.setViewportView(bookLayoutStrip);
        bookLayoutStrip.setLayout( new VerticalLayout() );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(bookLayoutStripView, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(bookLayoutStripView, javax.swing.GroupLayout.DEFAULT_SIZE, 867, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bookLayoutStrip;
    private javax.swing.JScrollPane bookLayoutStripView;
    // End of variables declaration//GEN-END:variables

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized BookLayoutTopComponent getDefault()
    {
        if ( instance == null )
        {
            instance = new BookLayoutTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the BookLayoutTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized BookLayoutTopComponent findInstance()
    {
        TopComponent win = WindowManager.getDefault().findTopComponent( PREFERRED_ID );
        if ( win == null )
        {
            Logger.getLogger( BookLayoutTopComponent.class.getName() ).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system." );
            return getDefault();
        }
        if ( win instanceof BookLayoutTopComponent )
        {
            return (BookLayoutTopComponent) win;
        }
        Logger.getLogger( BookLayoutTopComponent.class.getName() ).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior." );
        return getDefault();
    }

    @Override
    public int getPersistenceType()
    {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened()
    {
    }

    @Override
    public void componentClosed()
    {
    }

    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version", "1.0" );
        // TODO store your settings
    }

    @Override
    public void updateUI()
    {
        refresh();
        super.updateUI();
    }

    @Override
    public void paint( Graphics g )
    {
        super.paint( g );
    }

    Object readProperties( java.util.Properties p )
    {
        BookLayoutTopComponent singleton = BookLayoutTopComponent.getDefault();
        singleton.readPropertiesImpl( p );
        return singleton;
    }

    private void readPropertiesImpl( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID()
    {
        return PREFERRED_ID;
    }

    public Book getCurrentBook()
    {
        return currentBook;
    }

    public void setCurrentBook( Book currentBook )
    {
        this.currentBook = currentBook;
        refresh();
    }

    public void refresh()
    {
        currentBook = KurvyDataManager.getInstance().getCurrentBook();

        PageSpread[] spreadArray = currentBook.getPageSpreadArray();
        bookLayoutStrip.removeAll();
        System.out.println( "Spread Array size: " + spreadArray.length );
        for( int i = 0; i < spreadArray.length; i++ )
        {
            PageSpread spread = spreadArray[ i ];
            PageSpreadSlide slide = new PageSpreadSlide( spread );
            bookLayoutStrip.add( slide );
        }
    }
}
